using UnityEngine;
using QFramework;
using System;
using System.Collections;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class MPFive : Gun
	{
        //public override Bullet bulletObject => Bullet;

        public override AudioSource AudioSource => SelfAudioSource;

        public override float AtkTime => atkTime;


		private float atkTime = 0;

        private Vector2 dics;


        private void Start()
        {
            SetInputAction(() => Shooting(dics), 
                () => ShootDown(dics), 
                () => ShootUp(dics));

            RemoveBulletCount();
        }

        private void Update()
        {
            MouseDown();
        }

        public override void GetDic(Vector2 dic)
        {
            base.GetDic(dic);
            dics = dic;
        }

        #region 开枪逻辑

        public override void Shoot(Vector2 dic)
        {
            base.Shoot(dic);
            DataManager.Instance.mpFiveModel.CanFireIndex.Value -= 1;
            Debug.Log("mp5开火");
            var playerBullet = Instantiate(MpFiveBullet);
            playerBullet.transform.position = MpFiveBullet.transform.position;
            playerBullet.SetBulletSpeed(DataManager.Instance.mpFiveModel.MpFiveBulletSpeed.Value);
            playerBullet.BulletMoveDic(dic);
            playerBullet.Show();
           
        }



        public override void ShootDown(Vector2 dic)
        {
            base.ShootDown(dic);
            AudioSource.Play();
            if (DataManager.Instance.mpFiveModel.CanFireIndex.Value > 0)
            {
                Shoot(dics);
                AudioSource.clip = ShootSounds[0];
                AudioSource.loop = true;
                AudioSource.Play();
            }
           
        }

        public override void Shooting(Vector2 dic)
        {
            base.Shooting(dic);
            //Shoot(dic);
           
            if (atkTime>DataManager.Instance.mpFiveModel.
                MpFiveAtkSpeed.Value&&DataManager.Instance.mpFiveModel.CanFireIndex.Value>0)
            {
                atkTime = 0;
                Shoot(dic);
               // ShootDown(dic);
            }
            atkTime += Time.deltaTime;
        }
        public override void ShootUp(Vector2 dic)
        {
            base.ShootUp(dic);
            AudioSource.Stop();
        }
        #endregion

        #region 换弹逻辑
        public override void OnReloadStart()
        {
            base.OnReloadStart();
            Guns.Hide();
            RemoveAnim.Show();
            AudioSource.Stop();
        }
        public override void OnReloadFinished()
        {
            base.OnReloadFinished();
           
            RemoveAnim.Hide();
            Guns.Show();
            DataManager.Instance.mpFiveModel.CanFireIndex.Value = 
                DataManager.Instance.mpFiveModel.MpFiveBulletCount.Value;

        }

        protected override IEnumerator RemoveBullet(float time)
        {
            return base.RemoveBullet(time);
        }

        public override void RemoveBulletCount()
        {
            base.RemoveBulletCount();
            DataManager.Instance.mpFiveModel.CanFireIndex.Register(value =>
            {

                if (value <= 0)
                {
                    this.Delay(0.25f, () => StartCoroutine(RemoveBullet(DataManager.
                        Instance.mpFiveModel.MpFiveRemoveBulletSpeed.Value)));
                }

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }
        #endregion
    }
}
